﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.Resources;
using System.IO;
public partial class admin_LogoList : SitePage
{
    #region ViewState
    public int count = 0;
    public string paraURL
    {
        get
        {
            object o = ViewState["paraURL"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["paraURL"] = value; }

    }
    public int F_PageIndex
    {

        get
        {

            object o = ViewState["F_PageIndex"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["F_PageIndex"] = value; }

    }
    public int F_PageSize
    {

        get
        {

            object o = ViewState["F_PageSize"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["F_PageSize"] = value; }

    }
    public string F_SortField
    {

        get
        {
            object o = ViewState["F_SortField"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["F_SortField"] = value; }

    }
    public string F_SortExpression
    {

        get
        {
            object o = ViewState["F_SortExpression"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["F_SortExpression"] = value; }

    }    
    #endregion

    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitPosition();
            clsBindControl.BindBooleanCombobox(F_ddlIsAvtive);
            GetParamater();
            LoadData();
        }   
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);      
       
    }
    #endregion

    #region Events
    protected void Pager2_PageIndexChanging(object obj, PageIndexChangeEventArgs e)
    {
        this.F_PageIndex = e.PageIndex;
        LoadData();
    }
    protected void Pager2_PageSizeChanging(object obj, PageSizeChangeEventArgs e)
    {
        this.F_PageIndex = 1;
        this.F_PageSize=Pager2.PageSize = Pager1.PageSize = e.PageSize;
        LoadData();
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Logo objLogo = (Logo)e.Item.DataItem;
            if (objLogo == null)
                return;
            hidID.Value += objLogo.LogoId.ToString() + ";";

            //image button IsActive
            ImageButton imgButton = (ImageButton)e.Item.FindControl("imbActive");
            imgButton.CommandArgument = objLogo.LogoId.ToString();
            if (objLogo.IsActive == true)
            {
                imgButton.ImageUrl = clsString.ACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_DEACTIVE;
                imgButton.CommandName = "DeActive";
            }
            else
            {
                imgButton.ImageUrl = clsString.DEACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_ACTIVE;
                imgButton.CommandName = "Active";
            }
            //image button Order
            imgButton = (ImageButton)e.Item.FindControl("imbOrderDown");
            if (e.Item.ItemIndex + 1 == count)
                imgButton.Visible = false;
            else
            {
                imgButton.ToolTip = Resources.Resource.TIP_MOVEDOWN;
                imgButton.CommandArgument = objLogo.LogoId.ToString();
                imgButton.ImageUrl = clsString.ORDERDOWN_IMAGE_URL;
            }
            imgButton = (ImageButton)e.Item.FindControl("imbOrderUp");
            if (e.Item.ItemIndex == 0)
                imgButton.Visible = false;
            else
            {
                imgButton.CommandArgument = objLogo.LogoId.ToString();
                imgButton.ToolTip = Resources.Resource.TIP_MOVEUP;
                imgButton.ImageUrl = clsString.ORDERUP_IMAGE_URL; ;
                imgButton.Visible = true;
            }
            //Image
            Literal ltrImage = (Literal)e.Item.FindControl("ltrImage");
            if (ltrImage != null)
            {
                string pathImage = Server.MapPath("~/" + clsConfigData.DEFAULT_UPLOAD_LOGO_FOLDER) + objLogo.ImageFile;
                if (File.Exists(pathImage))
                {
                    ltrImage.Text = "<img src='" + "../" + clsConfigData.DEFAULT_UPLOAD_LOGO_FOLDER + objLogo.ImageFile + "' alt='" + objLogo.Title + "' height='80px' />";
                }
            }
            imgButton = (ImageButton)e.Item.FindControl("imbDelete");
            imgButton.ToolTip = Resources.Resource.TIP_DELETE;
            imgButton.Attributes.Add("onclick", "return confirm('" + Resources.Resource.MSG_CONFIRM_DELETE + "');");
            imgButton.CommandArgument = objLogo.LogoId.ToString();
            imgButton.ImageUrl = clsString.DELETE_IMAGE_URL;
            System.Web.UI.HtmlControls.HtmlTableRow row = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("trRow");
            if (e.Item.ItemIndex % 2 == 0)
                row.Attributes.Add("class", "alt-row");

        }

    }
    private void InitPosition()
    {
        foreach (string volume in Enum.GetNames(typeof(enmTemplatePosition)))
        {
            enmTemplatePosition veggie = (enmTemplatePosition)Enum.Parse(typeof(enmTemplatePosition), volume);
            int veggieValue = (int)veggie;
            F_drlPosition.Items.Add(new ListItem(volume, veggieValue.ToString()));
        }
    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName == "SortOrder")
        {
            SortList(e.CommandArgument.ToString());

        }
        else
        {
            int id = clsConvert.Int(e.CommandArgument.ToString());
            switch (e.CommandName)
            {

                case "OrderUp":
                    {
                        OrderUp(id);
                        break;
                    }
                case "OrderDown":
                    {
                        OrderDown(id);
                        break;
                    }
                case "Active":
                    {
                        Active(id);
                        break;
                    }
                case "DeActive":
                    {
                        DeActive(id);
                        break;
                    }

                case "Delete":
                    {
                        if (Delete(id))
                            LoadData();
                        break;
                    }
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.F_SortField = busLogo.SortOrder;
        this.F_SortExpression = "ASC";
        this.F_PageIndex = 1;
        LoadData();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string[] arr = hidIDSelect.Value.Split(';');
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i].ToString() != string.Empty)
            {
                Delete(clsConvert.Int(arr[i].ToString()));
            }
        }
        LoadData();

    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("LogoDetail.aspx?" + paraURL);
    }

    #endregion

    #region Methods
   
    private void SortList(string F_SortField)
    {
        if (this.F_SortField == F_SortField)
        {
            if (this.F_SortExpression == "ASC")
                this.F_SortExpression = "DESC";
            else this.F_SortExpression = "ASC";
        }
        else
        {
            this.F_SortField = F_SortField;
            this.F_SortExpression = "ASC";
        }
        LoadData();
    }
    private void OrderDown(int id)
    {
        busLogo objBus = new busLogo();
        if (objBus.OrderDown(id))
            LoadData();
    }
    private void OrderUp(int id)
    {

        busLogo objBus = new busLogo();
        if (objBus.OrderUp(id))
            LoadData();
    }
    private void Active(int id)
    {
        busLogo objBus = new busLogo();
        if (objBus.Active(id))
            LoadData();
    }
    private void DeActive(int id)
    {
        busLogo objBus = new busLogo();
        if (objBus.DeActive(id))
            LoadData();
    }
    private bool Delete(int id)
    {
        //get Image
        busLogo objBus = new busLogo();
        Logo objTip = objBus.GetByID(id);
        if (objTip != null)
        {
            //Delete Data
            if (objBus.Delete(id))
            {
                //Delete Image
                DeleteImage(objTip.ImageFile);
                return true;
            }
        }
        return false;
    }
    private void DeleteImage(string _orgImageFileName)
    {
        string path = Server.MapPath("~/" + clsConfigData.DEFAULT_UPLOAD_LOGO_FOLDER);
        clsFile.DeleteFile(path + _orgImageFileName);
        path = Server.MapPath("~/" + clsConfigData.DEFAULT_UPLOAD_LOGO_THUMBIMAGE_FOLDER);
        clsFile.DeleteFile(path + _orgImageFileName);
    }
    private void LoadData()
    {
        paraURL = GetPageParams(FilterFieldType.All);
        if (!paraURL.Contains("F_drlPosition="))
            paraURL = paraURL + "&F_drlPosition=" + F_drlPosition.SelectedValue;
        
        F_drlPosition.Enabled = false;

        hidID.Value = string.Empty;
        //get Condition
        Logo objLogo = new Logo();
        objLogo.CurrentPage = this.F_PageIndex;
        objLogo.OrderBy = this.F_SortField;
        objLogo.OrderDirection = this.F_SortExpression;
        objLogo.PageSize = Pager1.PageSize;
        objLogo.Condition = " l.[Position]=" + F_drlPosition.SelectedValue;
        if (F_txtTitle.Text != string.Empty)
            objLogo.Condition += " and l.Title like N'%" + F_txtTitle.Text + "%'";
        if (F_ddlIsAvtive.SelectedValue != string.Empty)
            objLogo.Condition += " and l.IsActive=" + F_ddlIsAvtive.SelectedValue.ToString();
        busLogo _busLogo = new busLogo();
        int total = 0;
        LogoCollection lstResult = _busLogo.List(objLogo);
        total = objLogo.TotalRecords;
        if (lstResult != null && lstResult.Count > 0)
        {
            count = lstResult.Count;
            rptData.DataSource = lstResult;
            divGrid.Visible = true;
            divEmplty.Visible = false;
        }
        else
        {
            rptData.DataSource = null;
            divGrid.Visible = false;
            divEmplty.Visible = true;
            divEmplty.InnerHtml = Resources.Resource.MSG_EMPTY;
        }

        rptData.DataSource = lstResult;
        rptData.DataBind();
        Pager1.SetPaging(this.F_PageIndex, count, total);
        Pager2.SetPaging(this.F_PageIndex, count, total);
    }
    private void GetParamater()
    {
        F_ddlIsAvtive.SelectedValue = clsQueryString.GetString("F_ddlIsAvtive");
        F_drlPosition.SelectedValue = clsQueryString.GetString("F_drlPosition");
        F_txtTitle.Text = clsQueryString.GetString("F_txtTitle");
        this.F_SortField = clsQueryString.GetString("F_SortField");
        if (this.F_SortField == string.Empty)
            this.F_SortField = busLogo.SortOrder;
        this.F_SortExpression = clsQueryString.GetString("F_SortExpression");
        if (this.F_SortExpression == string.Empty)
            this.F_SortExpression = "ASC";
        F_PageIndex = clsQueryString.GetInt("F_PageIndex");
        F_PageSize = clsQueryString.GetInt("F_PageSize");
        if (F_PageSize < 1)
            F_PageSize = clsConfigData.DEFAULT_ADMIN_PAGE_SIZE;
        if (F_PageIndex < 1)
            F_PageIndex = 1;
        this.Pager2.PageSize = this.Pager1.PageSize = F_PageSize;       
    }
    #endregion
}